package com.buka.goods.controller;


import com.buka.entity.PageResult;
import com.buka.entity.Result;
import com.buka.entity.StatusCode;
import com.buka.goods.service.BrandService;

import com.buka.server.pojo.Brand;
import com.buka.utils.TokenDecode;
import com.github.pagehelper.Page;
import com.itbuka.feign.MinioFeign;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/req")
@CrossOrigin
//@ComponentScan(basePackages = "/com/itbuka/utils")
public class GoodsBrandController {
    @Autowired
    BrandService brandService;
    @Autowired
    MinioFeign minioFeign;

    @Autowired
    private TokenDecode tokenDecode;

    @RequestMapping("/findall")
//    @PreAuthorize("hasAnyAuthority('a')")
    public Result<List<Brand>> findAll() {
        tokenDecode.getUserInfo();
        List<Brand> all = brandService.findAll();
        return new Result<>(true, StatusCode.OK, "查询成功", all);
    }

    @RequestMapping("/findby/{id}")
    public Result<List<Brand>> findById(@PathVariable("id") Long id) {
        List<Brand> all = brandService.findById(id);
        return new Result<>(true, StatusCode.OK, "ID查询成功", all);
    }

    @PostMapping("/add")
    public Result addBrand(@RequestBody Brand brand) {
        if (brand == null) {
            return new Result<>(false, StatusCode.ERROR, "数据错误");
        }
        int a = brandService.addBrand(brand);
        if (a != 1) {
            return new Result<>(false, StatusCode.ERROR, "插入失败");
        } else
            return new Result<>(true, StatusCode.OK, "插入数据成功");
    }

    @PostMapping("/update")
    public Result updateBrand(@RequestBody Brand brand) {
        if (brand == null) {
            return new Result<>(false, StatusCode.ERROR, "数据错误");
        }
        int a = brandService.updateBrand(brand);
        if (a != 1) {
            return new Result<>(false, StatusCode.ERROR, "更新失败");
        } else
            return new Result<>(true, StatusCode.OK, "更新数据成功");

    }

    @RequestMapping("/deleteTrue/{id}")
    public Result deleteTrueBrand(@PathVariable Long id) {
        if (id == null) {
            return new Result<>(false, StatusCode.ERROR, "未输入id");
        }
        int a = brandService.deleteTrueBrand(id);
        if (a != 1) {
            return new Result<>(false, StatusCode.ERROR, "删除失败");
        } else
            return new Result<>(true, StatusCode.OK, "删除数据成功");

    }

    @RequestMapping("/deleteFlase/{id}")
    public Result deleteFlaseBrand(@PathVariable Long id) {
        if (id == null) {
            return new Result<>(false, StatusCode.ERROR, "未输入id");
        }
        int a = brandService.deleteFalseBrand(id);
        if (a != 1) {
            return new Result<>(false, StatusCode.ERROR, "删除失败");
        } else
            return new Result<>(true, StatusCode.OK, "删除数据成功");
    }

    //    @RequestMapping("/select/{s}")
//    public Result<List<Brand>> FuzzyQueryByString(@PathVariable("s") String s){
//        if (s==null){
//            return new Result<>(false,StatusCode.ERROR,"String-Error");
//        }
//        List<Brand> list=brandService.FuzzyQuery(s);
//        return new Result<>(true,StatusCode.OK,"查到的结果",list);
//    }
    @GetMapping(value = "/search")
    public Result<List<Brand>> FuzzyQueryByString(@RequestParam String brandName) {

        List<Brand> list = brandService.FuzzyQuery(brandName);
        return new Result<>(true, StatusCode.OK, "查到的结果", list);
    }

    @RequestMapping("/controllerLogo/{id}")
    public Result OpenOrStopBrand(@PathVariable Long id) {
        int a = brandService.OpenOrStopBrand(id);
        if (a == 0) {
            return new Result<>(true, StatusCode.OK, "启用商标成功");
        } else
            return new Result<>(true, StatusCode.OK, "禁用商标成功");
    }

    @GetMapping(value = "/search/")
    public Result findPage(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<Brand> pagelist = brandService.findPage(page, size);
        PageResult pageResult = new PageResult(pagelist.getTotal(), pagelist.getResult());
        return new Result<>(true, StatusCode.OK, "分页显示成功", pageResult);
    }

    @GetMapping(value = "/searchtj/")
    public Result findPage(@RequestParam String brandName, @RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer size) {
        Page<Brand> pagelist = brandService.findPage(brandName, page, size);
        PageResult pageResult = new PageResult(pagelist.getTotal(), pagelist.getResult());
        return new Result<>(true, StatusCode.OK, "分页显示成功", pageResult);
    }

    @RequestMapping("/upload")
    public Result test1(@RequestParam String brandName, @RequestParam(name = "file") MultipartFile file) {

        int a = brandService.addAndFile(brandName, file);
        if (a != 1) {
            return new Result(false, StatusCode.ERROR, "error");
        } else
//        String message = (String) data;
            return new Result<>(true, StatusCode.OK, "插入（文件）成功！");
    }


}
